﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

using Qianwei.Model;
using Qianwei.Biz;
using Qianwei.Common.Extension;
using Qianwei.Common;

namespace Qianwei.Web
{
    public partial class AcceptJob : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SetTitle();
            }
        }

        private void SetTitle()
        {
            int jid = Request["jid"].TryInt();
            if (jid == 0)
            {
                lcontenttitle.Text = "未知标题";
                return;
            }
            IList<job_info> jobs = JobInfoManager.Instance.QueryJobInfo(string.Format("Job_Id={0}", jid));
            if (jobs == null || jobs.Count == 0)
            {
                lcontenttitle.Text = "未知标题";
                return;
            }
            job_info pagenew = jobs.First();
            lJobName.Text = lcontenttitle.Text = pagenew.Job_Position;
        }

        private string CheckInput()
        {
            string strErr = string.Empty;
            if (txtUser_Name.Text.Trim().Length == 0)
            {
                strErr = "请输入姓名";
            }
            else if (txtBirthday.Text.Trim().Length == 0)
            {
                strErr = "请输入出生日期";
            }
            else if (!System.Text.RegularExpressions.Regex.IsMatch(txtBirthday.Text.Trim(), @"^\d{4}-\d{1,2}-\d{1,2}$"))
            {
                strErr = "请输入正确格式的出生日期";
            }
            else if (txtUniversity.Text.Trim().Length == 0)
            {
                strErr = "请输入毕业院校";
            }
            else if (ddlEducation.Items.Count == 0)
            {
                strErr = "请选择学历";
            }
            else if (txtProfessional.Text.Trim().Length == 0)
            {
                strErr = "请输入专业";
            }
            else if (txtGraduation_Time.Text.Trim().Length == 0)
            {
                strErr = "请输入毕业时间";
            }
            else if (!System.Text.RegularExpressions.Regex.IsMatch(txtGraduation_Time.Text.Trim(), @"^\d{4}-\d{1,2}$"))
            {
                strErr = "请输入正确格式的毕业时间";
            }
            else if (txtTelePhone.Text.Trim().Length == 0)
            {
                strErr = "请输入电话";
            }
            else if (txtContact_Address.Text.Trim().Length == 0)
            {
                strErr = "请输入联系地址";
            }
            else if (txtAbility_Level.Text.Trim().Length == 0)
            {
                strErr = "请输入水平与能力";
            }
            else if (txtPersonal_Resume.Text.Trim().Length == 0)
            {
                strErr = "请输入个人简历";
            }
            return strErr;
        }

        private void SaveResume()
        {
            int jid = Request["jid"].TryInt();
            resume_info resumeInfo = new resume_info();
            resumeInfo.Ability_Level = txtAbility_Level.Text.Trim();
            resumeInfo.Birthday = txtBirthday.Text.Trim().TryDateTime();
            resumeInfo.Contact_Address = txtContact_Address.Text.Trim();
            resumeInfo.Create_Time = DateTime.Now;
            resumeInfo.Education = ddlEducation.SelectedItem.Text.Trim();
            resumeInfo.Email = txtEmail.Text.Trim();
            resumeInfo.Graduation_Time = txtGraduation_Time.Text.Trim().TryDateTime();
            resumeInfo.Job_Id = jid;
            resumeInfo.Marital_Status = ddlMarital_Status.SelectedItem.Text;
            resumeInfo.Personal_Resume = txtPersonal_Resume.Text.Trim();
            resumeInfo.Professional = txtProfessional.Text.Trim();
            resumeInfo.Sex = ddlSex.SelectedItem.Text;
            resumeInfo.TelePhone = txtTelePhone.Text.Trim();
            resumeInfo.University = txtUniversity.Text.Trim();
            resumeInfo.User_Name = txtUser_Name.Text.Trim();
            if (ResumeInfoManager.Instance.Add(resumeInfo) > 0)
            {
                Util.PageAlert("提交简历成功");
            }
            else
            {
                Util.PageAlert("对不起,提交简历失败,请稍后重试");
            }
        }
        #region Event

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string result = CheckInput();
            if (result.Length > 0)
            {
                Util.PageAlert(result);
            }
            else
            {
                SaveResume();
            }
        }
        #endregion
    }
}